//
//  MyCollectionViewController.m
//  1gene
//
//  Created by 1jiyin on 16/3/16.
//  Copyright © 2016年 1jiyin. All rights reserved.
//
//*******************我的收藏*******************
#import "MyCollectionViewController.h"
#import "ZiXunViewController.h"
#import "OnLineProductModel.h"
#import "OnLineProductViewController.h"

@interface MyCollectionViewController ()<MyCollectionTableViewDelegate,UIScrollViewDelegate,InformationArticleTableViewDelegate>
@property(nonatomic,copy)UIImageView *v1;
//@property(nonatomic,copy)UIImageView *v2;
@property(nonatomic,copy)UIImageView *v3;
@property(nonatomic,copy)UIImageView *v4;



@property(nonatomic,copy)UIScrollView *myScrollView;
@property(nonatomic,copy)UIView *view1;
@property(nonatomic,copy)UIView *view2;
@property(nonatomic,copy)UIView *view3;
@property(nonatomic,copy)UIView *view4;

@property(nonatomic,copy)UIScrollView *productScrollView;



@property(nonatomic,copy)NSArray *productArray;

@property(nonatomic,copy)NSArray *newsArray;

@property(nonatomic,copy)InformationArticleTableView *informationArticleTableView;

@property(nonatomic,strong)NSMutableArray *onLineProductArray;

@end

@implementation MyCollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:[UIView new]];
    [self createNavigation];
    [self getProduct];
    [self createUI];
    // Do any additional setup after loading the view.
}
-(void)createNavigation
{
    self.navigationController.navigationBar.hidden=NO;
    self.navigationController.navigationBar.barTintColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1];
    //返回
    UIButton *leftBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame=CGRectMake(0, 0, 25, 25);
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"left_back"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(leftBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    
    //标题
    UILabel *title=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 44)];
    title.text=@"收藏";
    title.font=[UIFont systemFontOfSize:22];
    title.textAlignment=NSTextAlignmentCenter;
    title.textColor=[UIColor whiteColor];
    self.navigationItem.titleView=title;
}
//返回上一页
-(void)leftBtnClick:(UIButton *)sender
{
    if (_barState==2) {
        self.navigationController.navigationBar.hidden=YES;
    }else{
        self.navigationController.navigationBar.hidden=NO;
    }
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)createUI
{
    CGFloat width=(self.view.frame.size.width-10*5)/4.0;
    CGFloat lab_w=40;
    CGFloat height=40;
    //产品
    UIImageView *v1=[[UIImageView alloc]initWithFrame:CGRectMake((self.view.frame.size.width-65*3-20*4)/2.0, 20+44+10, 20, 20)];
    v1.image=[UIImage imageNamed:@"my_yuan1"];
    [self.view addSubview:v1];
    UILabel *l1=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(v1.frame)+5, 20+44, lab_w, height)];
    l1.text=@"产品";
    l1.textColor=[UIColor colorWithRed:87/255.0 green:187/255.0 blue:215/255.0 alpha:1];
    l1.font=[UIFont systemFontOfSize:15];
    [self.view addSubview:l1];
    UIButton *b1=[UIButton buttonWithType:UIButtonTypeCustom];
    b1.frame=CGRectMake(0, 20+44, width, height);
    b1.tag=1;
    [b1 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:b1];
    
    UIImageView *v3=[[UIImageView alloc]initWithFrame:CGRectMake(width*2, 20+44+10, 20, 20)];
    v3.image=[UIImage imageNamed:@"11111111-9"];
    [self.view addSubview:v3];
    UILabel *l3=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(v3.frame)+5, 20+44, lab_w, height)];
    l3.text=@"资讯";
    l3.textColor=[UIColor colorWithRed:87/255.0 green:187/255.0 blue:215/255.0 alpha:1];
    l3.font=[UIFont systemFontOfSize:15];
    [self.view addSubview:l3];
    UIButton *b3=[UIButton buttonWithType:UIButtonTypeCustom];
    b3.frame=CGRectMake(width*2, 20+44, width, height);
    b3.tag=2;
    [b3 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:b3];
    
    
    UIImageView *v4=[[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(b3.frame)+20, 20+44+10, 20, 20)];
    v4.image=[UIImage imageNamed:@"11111111-9"];
    [self.view addSubview:v4];
    UILabel *l4=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(v4.frame)+5, 20+44, lab_w, height)];
    l4.text=@"帖子";
    l4.textColor=[UIColor colorWithRed:87/255.0 green:187/255.0 blue:215/255.0 alpha:1];
    l4.font=[UIFont systemFontOfSize:15];
    [self.view addSubview:l4];
    UIButton *b4=[UIButton buttonWithType:UIButtonTypeCustom];
    b4.frame=CGRectMake(width*3, 20+44, width, height);
    b4.tag=3;
    [b4 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:b4];
    
    
    _v1=v1;
    //_v2=v2;
    _v3=v3;
    _v4=v4;
    
    
    
    
    _myScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(l1.frame), self.view.frame.size.width, self.view.frame.size.height-CGRectGetMaxY(l1.frame))];
    _myScrollView.showsVerticalScrollIndicator=NO;
    _myScrollView.contentSize=CGSizeMake(self.view.frame.size.width*3, _myScrollView.frame.size.height);
    _myScrollView.pagingEnabled=YES;
    _myScrollView.delegate=self;
    _myScrollView.showsHorizontalScrollIndicator=NO;
    _myScrollView.bounces=NO;
    [self.view addSubview:_myScrollView];
    
    
    _view1=[[UIView alloc]initWithFrame:CGRectMake(0, 0, _myScrollView.frame.size.width, _myScrollView.frame.size.height)];
    _view1.backgroundColor=[UIColor whiteColor];
    [_myScrollView addSubview:_view1];
    //    UIButton *viewBtn1=[UIButton buttonWithType:UIButtonTypeCustom];
    //    viewBtn1.frame=_view1.frame;
    //    [viewBtn1 setBackgroundImage:[UIImage imageNamed:@"my_chanpin"] forState:UIControlStateNormal];
    //    [_view1 addSubview:viewBtn1];
    
       
    _view3=[[UIView alloc]initWithFrame:CGRectMake(_myScrollView.frame.size.width*1, 0, _myScrollView.frame.size.width, _myScrollView.frame.size.height)];
    _view3.backgroundColor=[UIColor grayColor];
    [_myScrollView addSubview:_view3];
    
    _view4=[[UIView alloc]initWithFrame:CGRectMake(_myScrollView.frame.size.width*2, 0, _myScrollView.frame.size.width, _myScrollView.frame.size.height)];
    _view4.backgroundColor=[UIColor grayColor];
    [_myScrollView addSubview:_view4];
    
    InformationArticleTableView *informationArticleTableView=[[InformationArticleTableView alloc]initWithFrame:_view3.bounds style:UITableViewStylePlain];
    informationArticleTableView.myDelegate=self;
    [_view3 addSubview:informationArticleTableView];
    _informationArticleTableView=informationArticleTableView;
    
    [self createView4];
    
}
//产品点击
-(void)view1BtnClick:(UIButton *)sender
{
    NSLog(@".........");
}



//
-(void)btnClick:(UIButton *)sender
{
    [self changeLocation:sender.tag];
}
-(void)changeLocation:(NSInteger)index
{
    if (index==1) {
        _v1.image=[UIImage imageNamed:@"my_yuan1"];
        //_v2.image=[UIImage imageNamed:@"11111111-9"];
        _v3.image=[UIImage imageNamed:@"11111111-9"];
        _v4.image=[UIImage imageNamed:@"11111111-9"];
        _myScrollView.contentOffset=CGPointMake(_myScrollView.frame.size.width*0, 0);
        // NSLog(@"1");
    }else if (index==2){
        //NSLog(@"2");
        //_v2.image=[UIImage imageNamed:@"my_yuan1"];
        _v1.image=[UIImage imageNamed:@"11111111-9"];
        _v3.image=[UIImage imageNamed:@"my_yuan1"];
        _v4.image=[UIImage imageNamed:@"11111111-9"];
        _myScrollView.contentOffset=CGPointMake(_myScrollView.frame.size.width*1, 0);
    }else if (index==3){
        //NSLog(@"3");
        _v3.image=[UIImage imageNamed:@"11111111-9"];
        _v1.image=[UIImage imageNamed:@"11111111-9"];
        //_v2.image=[UIImage imageNamed:@"11111111-9"];
        _v4.image=[UIImage imageNamed:@"my_yuan1"];
        _myScrollView.contentOffset=CGPointMake(_myScrollView.frame.size.width*2, 0);
    }
}
//确认修改
-(void)changeBtnClick:(UIButton *)sender
{
    
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index=scrollView.contentOffset.x/_myScrollView.frame.size.width;
    index+=1;
    [self changeLocation:index];
}

-(void)createView4
{
    MyCollectionTableView *table=[[MyCollectionTableView alloc]initWithFrame:_view4.bounds style:UITableViewStylePlain];
    table.myCollectionDelegate=self;
    [_view4 addSubview:table];
}

//MyCollectionTableView的代理
-(void)gotoTieBaXiangQing:(NSString *)topicId
{
    TieBaXiangQingViewController *vc=[[TieBaXiangQingViewController alloc]init];
    vc.topic_id=[topicId integerValue];
    //vc.arr=[self getData];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark 获取数据
-(NSArray *)getData
{
     NSArray *arr=@[@{@"userHeadImage":@"community-backgroundImage.png",@"sex":@"♀",@"name":@"张三111111",@"biaoqian":@"标签",@"comments":@"100",@"zan":@"36100",@"time":@"01-22 15:33",@"state":@"精华帖",@"biaoti":@"帖子标题帖子标题帖子标题帖子标题帖子标题帖子标题帖子标题帖子标题",@"content":@"帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容",@"imageArr":@[@"myBackground.jpg",@"myBackgroundImage.png",@"myBackground.jpg",@"myBackground.jpg"]},@{@"userHeadImage":@"community-backgroundImage.png",@"sex":@"♀",@"name":@"张三",@"biaoqian":@"楼主",@"comments":@"100",@"zan":@"1000",@"time":@"01-22 15:33",@"state":@"精华帖",@"biaoti":@"帖子标题",@"content":@"帖子的内容",@"imageArr":@[@"myBackground.jpg",@"myBackground.jpg",@"myBackground.jpg"]},@{@"userHeadImage":@"community-backgroundImage.png",@"sex":@"♀",@"name":@"张三111111",@"biaoqian":@"标签",@"comments":@"100",@"zan":@"36100",@"time":@"01-22 15:33",@"state":@"精华帖",@"biaoti":@"帖子标题帖子标题帖子标题帖子标题帖子标题帖子标题帖子标题帖子标题",@"content":@"帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容帖子的内容",@"imageArr":@[@"myBackground.jpg",@"myBackgroundImage.png",@"myBackground.jpg",@"myBackground.jpg"]}];
    return arr;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//获取收藏的产品
-(void)getProduct
{
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    NSLog(@"收藏产品的userId====%@",_userId);
    [manager GET:[NSString stringWithFormat:GetCollectedProduct,_userId] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"收藏的产品%@",dic);
        if (dic) {
            _productArray=[NSArray arrayWithArray:dic[@"productions"]];
            _newsArray=[NSArray arrayWithArray:dic[@"newss"]];
            
            NSArray *productionArray = [NSArray arrayWithArray:_productArray];
            _onLineProductArray=[NSMutableArray array];
            for (NSDictionary *tempDic in productionArray) {
                OnLineProductModel *onLineModel = [[OnLineProductModel alloc] init];
                [onLineModel setValuesForKeysWithDictionary:tempDic];
                [self.onLineProductArray addObject:onLineModel];
            }
            
            
            
            
            dispatch_async(dispatch_get_main_queue(), ^{
                UIScrollView *productScrollView = [[UIScrollView alloc]initWithFrame:_view1.bounds];
                productScrollView.showsVerticalScrollIndicator=NO;
                [_view1 addSubview:productScrollView];
                
                CGFloat img_w=_view1.frame.size.width-20*2;
                CGFloat img_h=202;
                
                productScrollView.contentSize=CGSizeMake(productScrollView.frame.size.width, (img_h+20)*_productArray.count);
                
                for (int i=0; i<_productArray.count; i++) {
                    UIImageView *imgView=[[UIImageView alloc]initWithFrame:CGRectMake(20, (img_h+20)*i, img_w, img_h)];
                    imgView.tag=666+i;
                    NSLog(@"得到的产品的图片====%@",_productArray[i][@"advertis_img_url"]);
                    //_productArray[i][@"unlock_img_url"]
                    NSString *str=_productArray[i][@"advertis_img_url"];
                    str=[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
                    [imgView sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"noImage_background"]];
                    [productScrollView addSubview:imgView];
                    
                    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(view1TapHappen:)];
                    imgView.userInteractionEnabled=YES;
                    [imgView addGestureRecognizer:tap];
                    
                }
                
                
                //InformationArticleTableView *informationArticleTableView
                _informationArticleTableView.dataArray=[NSMutableArray arrayWithArray:_newsArray];
                [_informationArticleTableView reloadData];
                
                
            });
            
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"收藏产品error%@",error.localizedDescription);
    }];
    
}

//产品详情
-(void)view1TapHappen:(UITapGestureRecognizer *)tap
{
    UIImageView *imgView=(id)tap.view;
    NSLog(@"点击了哪一个%ld",imgView.tag);
    
    OnLineProductViewController *onLineProductVC = [[OnLineProductViewController alloc] init];
    OnLineProductModel *model = self.onLineProductArray[imgView.tag-666];
    onLineProductVC.onLineProductName = model.name;
    onLineProductVC.onLineProductID = [NSString stringWithFormat:@"%@", model.myID];
    [self.navigationController pushViewController:onLineProductVC animated:YES];
    
}

//咨询详情
-(void)returnTitleId:(NSString *)titleId
{
    NSLog(@"==========%@",titleId);
    ZiXunViewController *vc=[ZiXunViewController new];
    vc.ziXunID=[NSString stringWithFormat:@"%@",titleId];
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)viewWillAppear:(BOOL)animated
{
     [self.navigationController.navigationBar setTranslucent:YES];
}


@end
